def prepare_matrix(data):
    result=[]
    for i in range(0,len(data)-1,1):
        tmp=[]
        for j in range(0,(len(data[i])-1), 1):
            tmp.append([data[i][j], data[i][j+1], data[i+1][j+1], data[i+1][j]])            
        result.append(tmp)
    return result
    
def calculate_dots(prepared_matrix, intervals):
    result=[]
    
    for y in prepared_matrix:
        tmp=[]
        for x in y:
            tmp0=[]
            tmp1=[]
            tmp2=[]
            tmp3=[]
            
            tmp0=calculate_partials([x[0],x[1]], intervals,0)
            tmp1=calculate_partials([x[1],x[2]], intervals,1)
            tmp2=calculate_partials([x[2],x[3]], intervals,2)
            tmp3=calculate_partials([x[3],x[0]], intervals,3)
            
            tmp.append([tmp0,tmp1,tmp2,tmp3])            
        
        result.append(tmp)
    return result
        
def calculate_partials(data,intervals,brink):
    result=[]    
    if data[1]> data[0]:
        lst=[i for i in intervals if not i<data[0] and i<data[1]]
        for i in lst:
            j= float(i-data[0]) / float(data[1]-data[0])
            if brink in [2,3]: j=1-j
            result.append([j,i])
    elif data[1]< data[0]:
        lst=[i for i in intervals if not i<data[1] \
                                                    and i<data[0]]
        lst.reverse()
        for i in lst:
            j= float(i-data[1]) / float(data[0]-data[1])
            if brink in [0,1] :   j=1-j
            result.append([j,i])
    return result

def calculate_coords(data):
    result=[]
    for y in range(len(data)):
        tmp_=[]
        for x in range(len(data[y])):
            tmp=[]
            vallist=[]
            partlist=[]
            brinklist=[]
            
            brink=0
            #  forming of a common list from separate lists for each brink
            while brink<4:
                for i in data[y][x][brink]:
                    partlist.append(i[0])
                    vallist.append(i[1])
                    brinklist.append(brink)
                    
                brink+=1    
            #  searching of lines in common list
            while len(vallist)>0:
                val=vallist[0];vallist.pop(0)
                part=partlist[0];partlist.pop(0)
                brink=brinklist[0];brinklist.pop(0)
                
                index=vallist.index(val)
                vallist.pop(index)
                part_=partlist[index];partlist.pop(index)
                brink_=brinklist[index];brinklist.pop(index)
                
                #  calculation of coordinates                
                x1,y1 = relat_coords(y,x,part,brink)
                x2,y2 = relat_coords(y,x,part_,brink_)
                
                if (x1>x2) and (y1>y2):
                    tmp.append([val,[y1,x1],[y2,x2]])
                else:
                    tmp.append([val,[y2,x2],[y1,x1]])               
                
            result.append(tmp)
    return result
    
def relat_coords(y,x,part,brink):
    if brink==0:
        y1 = y
        x1 = x+part
    elif brink==1:
        y1 = y+part
        x1 = x+1
    elif brink==2:
        y1 = y+1
        x1 = x+part
    elif brink==3:
        y1 = y+part
        x1 = x
    return y1+0.5, x1+0.5

def collect_lines(data):
    curves=[]
    for y in range(len(data)):
        for x in range(len(data[y])):
            if data[y][x]==[]: continue
            val = data[y][x][0]
            x1, y1 = data[y][x][1][0], data[y][x][1][1]
            x2, y2 = data[y][x][2][0], data[y][x][2][1]
            is_a_new_line = True
            for i in range(len(curves)):
                curve=curves[i]
                if [val, x1, y1] == [curve[0], curve[1][-1][0], curve[1][-1][1]]:
                    curves[i][1].append([x2,y2])
                    is_a_new_line=False
                elif [val, x2, y2] == [curve[0], curve[1][-1][0], curve[1][-1][1]]:
                    curves[i][1].append([x1,y1])
                    is_a_new_line=False
                elif [val, x1, y1] == [curve[0], curve[1][0][0], curve[1][0][1]]:
                    curves[i][1].insert(0,[x2,y2])                
                    is_a_new_line=False
                elif [val, x2, y2] == [curve[0], curve[1][0][0], curve[1][0][1]]:
                    curves[i][1].insert(0,[x1,y1])                
                    is_a_new_line=False
            if is_a_new_line==True:
                curves.append([val, [[x1, y1], [x2, y2]]])
    return curves
